﻿using BasicComponents.tree;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BasicComponents.tree
{
    /// <summary>
    /// CkboxTreeView.xaml 的交互逻辑
    /// </summary>
    public partial class CheckBoxTreeView : UserControl
    {
        public CheckBoxTreeView()
        {
            InitializeComponent();
        }
        private TreeView treeView;
        #region 私有变量属性
        /// <summary>
        /// 控件数据
        /// </summary>
        private ObservableCollection<TreeModel> _itemsSourceData;
        //用于搜索功能，保存原始的数据源
        private ObservableCollection<TreeModel> _itemsSourceDataRes;
        //用于搜索功能，保存已经搜索过的数据
        private ObservableCollection<TreeModel> searchedItems = new ObservableCollection<TreeModel>();
        #endregion

        /// <summary>
        /// 构造
        /// </summary> 
        /// <summary>
        /// 控件数据
        /// </summary>
        public ObservableCollection<TreeModel> ItemsSourceData
        {
            get { return _itemsSourceData; }
            set
            {
                _itemsSourceData = value;
                Binding binding = new Binding(".");
                binding.Source = this._itemsSourceData;
                binding.Mode = BindingMode.TwoWay;
                this.tvZsmTree.SetBinding(TreeView.ItemsSourceProperty, binding);
            }
        }
        /// <summary>
        /// 构造
        /// </summary> 
        /// <summary>
        /// 用于搜索的数据源变化
        /// </summary>
        private ObservableCollection<TreeModel> ItemsSourceDataForSearch
        {
            get { return _itemsSourceData; }
            set
            {
                Binding binding = new Binding(".");
                binding.Source = value;
                binding.Mode = BindingMode.TwoWay;
                this.tvZsmTree.SetBinding(TreeView.ItemsSourceProperty, binding);
            }
        }

        public TreeView TreeView
        {
            get
            {
                return tvZsmTree;
            }

            set
            {
                tvZsmTree = value;
            }
        }/// <summary>
         /// 设置对应树结构的右键内容
         /// </summary>
         /// <param name="id"></param>
         /// <returns></returns>
        public ContextMenu ContextMenuForTree
        {
            get
            {
                return treeContextMenu;
            }

            set
            {
                treeContextMenu = value;
            }
        }
        //
        public Boolean IsShowCheckBox
        {
            get
            {
                return this.ckb_showCkbox.IsChecked.Value;
            }

            set
            {
                this.ckb_showCkbox.IsChecked = value;
            }
        }
        public Visibility IsShowButtomExtraPanel
        {
            get
            {
                return this.stackPanel_bottom.Visibility;
            }

            set
            {
                this.stackPanel_bottom.Visibility = value;
            }
        }

        public Boolean IsShowID
        {
            get
            {
                return this.ckb_showId.IsChecked.Value;
            }

            set
            {
                this.ckb_showId.IsChecked = value;
            }
        }
        /// <summary>
        /// 设置对应Id的项为选中状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int SetCheckedById(string id, IList<TreeModel> treeList)
        {
            foreach (var tree in treeList)
            {
                if (tree.Id.Equals(id))
                {
                    tree.IsChecked = true;
                    return 1;
                }
                if (SetCheckedById(id, tree.Children) == 1)
                {
                    return 1;
                }
            }

            return 0;
        }
        /// <summary>
        /// 设置对应Id的项为选中状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int SetCheckedById(string id)
        {
            foreach (var tree in ItemsSourceData)
            {
                if (tree.Id.Equals(id))
                {
                    tree.IsChecked = true;
                    return 1;
                }
                if (SetCheckedById(id, tree.Children) == 1)
                {
                    return 1;
                }
            }

            return 0;
        }

        /// <summary>
        /// 获取选中项
        /// </summary>
        /// <returns></returns>
        public IList<TreeModel> CheckedItemsIgnoreRelation()
        {

            return GetCheckedItemsIgnoreRelation(_itemsSourceData);
        }

        /// <summary>
        /// 私有方法，忽略层次关系的情况下，获取选中项
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private IList<TreeModel> GetCheckedItemsIgnoreRelation(IList<TreeModel> list)
        {
            IList<TreeModel> treeList = new List<TreeModel>();
            foreach (var tree in list)
            {
                if (tree.IsChecked)
                {
                    treeList.Add(tree);
                }
                foreach (var child in GetCheckedItemsIgnoreRelation(tree.Children))
                {
                    treeList.Add(child);
                }
            }
            return treeList;
        }

        /// <summary>
        /// 选中所有子项菜单事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuSelectAllChild_Click(object sender, RoutedEventArgs e)
        {
            if (tvZsmTree.SelectedItem != null)
            {
                TreeModel tree = (TreeModel)tvZsmTree.SelectedItem;
                tree.IsChecked = true;
                foreach (TreeModel child in tree.Children)
                {
                    tree.SetChildrenChecked(child, true);
                }
            }
        }

        /// <summary>
        /// 全部展开菜单事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuExpandAll_Click(object sender, RoutedEventArgs e)
        {
            if (tvZsmTree.ItemsSource != null)
            {
                foreach (TreeModel tree in tvZsmTree.ItemsSource)
                {
                    tree.IsExpanded = true;
                    tree.SetChildrenExpanded(tree, true);
                }
            }
        }
        /// <summary>
        /// 全部折叠菜单事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuUnExpandAll_Click(object sender, RoutedEventArgs e)
        {
            if (tvZsmTree.ItemsSource != null)
            {
                foreach (TreeModel tree in tvZsmTree.ItemsSource)
                {
                    tree.IsExpanded = false;
                    tree.SetChildrenExpanded(tree, false);
                }
            }
        }

        /// <summary>
        /// 全部选中事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuSelectAll_Click(object sender, RoutedEventArgs e)
        {
            if (tvZsmTree.ItemsSource != null)
            {
                foreach (TreeModel tree in tvZsmTree.ItemsSource)
                {
                    tree.IsChecked = true;
                    foreach (TreeModel child in tree.Children)
                    {
                        tree.SetChildrenChecked(child, true);
                    }
                }
            }
        }

        /// <summary>
        /// 全部取消选中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuUnSelectAll_Click(object sender, RoutedEventArgs e)
        {
            if (tvZsmTree.ItemsSource != null)
            {
                foreach (TreeModel tree in tvZsmTree.ItemsSource)
                {
                    tree.IsChecked = false;
                    foreach (TreeModel child in tree.Children)
                    {
                        tree.SetChildrenChecked(child, false);
                    }
                }
            }
        }

        /// <summary>
        /// 鼠标右键事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem item = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
            if (item != null)
            {
                item.Focus();
                e.Handled = true;
            }
        }
        static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
        {
            while (source != null && source.GetType() != typeof(T))
                source = VisualTreeHelper.GetParent(source);

            return source;
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if (tvZsmTree.SelectedItem != null)
            {
                TreeModel tree = (TreeModel)tvZsmTree.SelectedItem;
                tree.IsChecked = true;
                foreach (TreeModel child in tree.Children)
                {
                    tree.SetChildrenChecked(child, true);
                }
            }
        }
        private void loopCheckItems(IList<TreeModel> listTreeModel, Boolean isChecked)
        {
            foreach (TreeModel treeModel in listTreeModel)
            {
                treeModel.IsChecked = isChecked;
                loopCheckItems(treeModel.Children, isChecked);
            }
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            if (tvZsmTree.SelectedItem != null)
            {
                TreeModel tree = (TreeModel)tvZsmTree.SelectedItem;
                tree.IsChecked = false;
                foreach (TreeModel child in tree.Children)
                {
                    tree.SetChildrenChecked(child, false);
                }
            }
        }
        //改变选项框显示状态
        private void changeCkboxVisibility(ObservableCollection<TreeModel> listTreeModel, Visibility visibility)
        {
            if (listTreeModel == null) return;
            foreach (TreeModel treeMode in listTreeModel)
            {
                treeMode.VisibilityCheckBox = visibility;
                if (treeMode.Children != null)
                {
                    changeCkboxVisibility(treeMode.Children, visibility);
                }
            }
        }
        //改变代码和代码分隔符显示状态
        private void changeIdVisibility(ObservableCollection<TreeModel> listTreeModel, Visibility visibility)
        {
            if (listTreeModel == null) return;
            foreach (TreeModel treeMode in listTreeModel)
            {
                treeMode.VisibilityId = visibility;
                treeMode.VisibilitySpliter = visibility;
                if (treeMode.Children != null)
                {
                    changeIdVisibility(treeMode.Children, visibility);
                }
            }
        }

        private void ckb_showCkbox_Checked(object sender, RoutedEventArgs e)
        {
            changeCkboxVisibility(this._itemsSourceData, Visibility.Visible);
        }
        private void ckb_showCkbox_Unchecked(object sender, RoutedEventArgs e)
        {
            changeCkboxVisibility(this._itemsSourceData, Visibility.Collapsed);
        }
        private void ckb_showId_Checked(object sender, RoutedEventArgs e)
        {
            changeIdVisibility(this._itemsSourceData, Visibility.Visible);
        }

        private void ckb_showId_Unchecked(object sender, RoutedEventArgs e)
        {
            changeIdVisibility(this._itemsSourceData, Visibility.Collapsed);
        }

        public void ShowCheckBox()
        {
            changeCkboxVisibility(this._itemsSourceData, Visibility.Visible);
        }

        public void HideCheckBox()
        {
            changeCkboxVisibility(this._itemsSourceData, Visibility.Collapsed);
        }
        public void ShowID()
        {
            changeIdVisibility(this._itemsSourceData, Visibility.Visible);
        }
        public void HideID()
        {
            changeIdVisibility(this._itemsSourceData, Visibility.Collapsed);
        }
        /// <summary>
        //添加节点。
        /// </summary>
        /// <param name="TreeModel"></param>
        /// <param name="preDM"></param> 
        /// <returns name="TreeModel"></returns>
        public static Boolean addTreeModelToList(ObservableCollection<TreeModel> treeNodelList, TreeModel newTreeNode, String preNodeID, Boolean hasNamedAllNode)
        {
            String id = newTreeNode.Id;
            String preDM = preNodeID;
            if (preDM != null && preDM.Equals(""))
            {
                //如果有所有代码这个级次，则添加到所有这个级次下，否则增加在跟目录下。
                if (hasNamedAllNode)
                {
                    if (treeNodelList.Count <= 0)
                    {
                        TreeModel root = new TreeModel();
                        root.Id = "";
                        root.Header = "所有";
                        treeNodelList.Add(root);
                    }
                    newTreeNode.Parent = treeNodelList[0];
                    treeNodelList[0].Children.Add(newTreeNode);
                    if (newTreeNode.IsExpanded && newTreeNode.Parent != null)
                    {
                        expanedParentNode(newTreeNode.Parent);
                    }
                }
                else
                {
                    treeNodelList.Add(newTreeNode);
                }
            }
            else if (preDM != null)
            {
                foreach (TreeModel treeModel in treeNodelList)
                {
                    if (preDM.StartsWith(treeModel.Id))
                    {
                        TreeModel treePreNode = loopTreeModel(treeModel, preDM);

                        //  MessageBox.Show("从循环中获取的节点:"+treePreNode);
                        if (treePreNode != null)
                        {
                            //  MessageBox.Show("从循环中获取的节点不为空" );
                            newTreeNode.Parent = treePreNode;
                            treePreNode.Children.Add(newTreeNode);
                            if (newTreeNode.IsExpanded && newTreeNode.Parent != null)
                            {
                                expanedParentNode(newTreeNode.Parent);
                            }
                            break;
                        }
                        else
                        {
                            // MessageBox.Show("从循环中获取的节点为空，循环即将继续。");
                            continue;
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            return true;
        }
        /// <summary>
        ///根据给定的ID返回指定的节点
        /// </summary>
        /// <param name="itemList"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static TreeModel getTreeModelByID(ObservableCollection<TreeModel> itemList, String id)
        {
            TreeModel target = null;
            foreach (TreeModel treeModel in itemList)
            {
                target = getTreeModel(treeModel, id);
                if (target != null) break;
            }
            return target;
        }
        private static TreeModel getTreeModel(TreeModel treeModel, String id)
        {
            if (treeModel.Id.Equals(id))
            {
                return treeModel;
            }
            else if ((treeModel.Id == null || treeModel.Id.Equals("") || id.StartsWith(treeModel.Id)) && treeModel.Children.Count > 0)
            {
                foreach (TreeModel treeModel_ in treeModel.Children)
                {
                    TreeModel temp = getTreeModel(treeModel_, id);
                    if (temp != null) return temp;
                }
            }
            return null;
        }
        /// <summary>
        ///根据给定的名称返回指定的集合
        /// </summary>
        /// <param name="itemList"></param>
        /// <param name="mc"></param>
        /// <returns></returns>
        public ObservableCollection<TreeModel> getTreeModelByMC(ObservableCollection<TreeModel> itemList, String mc)
        {
            searchedItems.Clear();//清空选择项
            TreeModel target = null;
            foreach (TreeModel treeModel in itemList)
            {
                target = getTreeModelMC(treeModel, mc);
                if (target != null) break;
            }
            return searchedItems;
        }
        private TreeModel getTreeModelMC(TreeModel treeModel, String mc)
        {
            if (treeModel.Header.IndexOf(mc) >= 0 || NPinyin.Pinyin.GetInitials(treeModel.Header).ToUpper().IndexOf(mc.ToUpper()) >= 0)
            {
                Boolean hasExists = false;
                foreach (TreeModel tr in searchedItems)
                {
                    if (tr.Id.Equals(treeModel.Id)) hasExists = true;
                    break;
                }
                if (!hasExists)
                {
                    searchedItems.Add(treeModel);
                }
            }
            if (treeModel.Children.Count > 0)
            {
                foreach (TreeModel treeModel_ in treeModel.Children)
                {
                    TreeModel temp = getTreeModelMC(treeModel_, mc);
                    if (temp != null) return temp;
                }
            }
            return null;
        }

        //展开上级项目
        private static void expanedParentNode(TreeModel treeModel)
        {
            if (treeModel.Parent != null)
            {
                treeModel.IsExpanded = true;
            }
            if (treeModel.Parent != null)
            {
                expanedParentNode(treeModel.Parent);
            }
        }
        /// <summary>
        //修改节点内容。
        /// </summary>
        /// <param name="TreeModel"></param>
        /// <param name="preDM"></param> 
        /// <returns name="TreeModel"></returns>
        public static Boolean updateTreeModelNode(ObservableCollection<TreeModel> treeNodelList, TreeModel newTreeNode, Boolean hasNamedAllNode)
        {
            String id = newTreeNode.Id;
            String nodeID = newTreeNode.Id;
            if (nodeID != null && nodeID.Equals(""))
            {
                //如果有所有代码这个级次，则添加到所有这个级次下，否则增加在跟目录下。
                if (hasNamedAllNode)
                {
                    treeNodelList[0].Header = newTreeNode.Header;
                    treeNodelList[0].ToolTip = newTreeNode.ToolTip;
                }
            }
            else if (nodeID != null)
            {
                foreach (TreeModel treeModel in treeNodelList)
                {
                    if (nodeID.StartsWith(treeModel.Id))
                    {
                        TreeModel treePreNode = loopTreeModel(treeModel, nodeID);
                        if (treePreNode != null)
                        {
                            treePreNode.Header = newTreeNode.Header;
                            treePreNode.ToolTip = newTreeNode.ToolTip;
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            return true;
        }

        /// <summary>
        //获取勾选中的节点内容。
        /// </summary>
        /// <param name="TreeModel"></param>
        /// <param name="preDM"></param> 
        /// <returns name="TreeModel"></returns>
        public static ObservableCollection<TreeModel> getCheckedTreeModelNode(ObservableCollection<TreeModel> treeNodelList)
        {
            ObservableCollection<TreeModel> resultList = new ObservableCollection<TreeModel>();
            foreach (TreeModel treeModel in treeNodelList)
            {
                if (treeModel.IsChecked)
                {
                    resultList.Add(treeModel);
                }
                if (treeModel.Children != null && treeModel.Children.Count > 0)
                {
                    loopGetCheckedTreeModel(resultList, treeModel, true);
                }
            }
            return resultList;
        }
        //循环获取指定内容treeModel的preDM 
        private static void loopGetCheckedTreeModel(ObservableCollection<TreeModel> resultList, TreeModel treeModel, Boolean isChecked)
        {
            if (isChecked.Equals(treeModel.IsChecked))
            {
                resultList.Add(treeModel);
            }
            if (treeModel.Children != null && treeModel.Children.Count > 0)
            {
                foreach (TreeModel node in treeModel.Children)
                {
                    loopGetCheckedTreeModel(resultList, node, isChecked);
                }
            }
        }
        /// <summary>
        //删除指定节点内容。
        /// </summary>
        /// <param name="TreeModel"></param>
        /// <param name="preDM"></param> 
        /// <returns name="TreeModel"></returns>
        //public static Boolean deleteTreeModelNode(ObservableCollection<TreeModel> treeNodelList, String nodeID)
        //{
        //    {
        //        foreach (TreeModel treeModel in treeNodelList)
        //        {
        //            if (nodeID.StartsWith(treeModel.Id))
        //            {
        //                TreeModel treeNode = loopTreeModel(treeModel, nodeID);
        //                if (treeNode != null)
        //                {
        //                    if (treeNode.Parent == null)
        //                    {
        //                        treeNodelList.Remove(treeNode);
        //                    }
        //                    else
        //                    {
        //                        treeNode.Parent.Children.Remove(treeNode);
        //                    }
        //                    break;
        //                }
        //                else
        //                {
        //                    continue;
        //                }
        //            }
        //            else
        //            {
        //                continue;
        //            }
        //        }
        //    }
        //    return true;
        //}
        //循环获取treeModel的preDM 
        public static TreeModel loopTreeModel(TreeModel treeModel, String preDM)
        {
            TreeModel result = null;
            if (preDM.Equals(treeModel.Id))
            {
                result = treeModel;
                return result;
            }
            else if (preDM.StartsWith(treeModel.Id))
            {
                foreach (TreeModel treeModel_ in treeModel.Children)
                {
                    // MessageBox.Show(treeModel_.Id);
                    result = loopTreeModel(treeModel_, preDM);
                    if (result != null) { break; }
                    else { continue; }
                }
            }

            //  MessageBox.Show("返回result"+result);
            return result;
        }

        /// <summary>
        //删除节点内容。
        /// </summary>
        /// <param name="list"></param>
        /// <param name="treeModel"></param> 
        /// <returns name="isChecked"></returns>
        public static void removeTreeModelByID(ObservableCollection<TreeModel> list, String nodeID)
        {
            //  MessageBox.Show("nodeID:" + nodeID+";list size:"+list.Count);
            for (int i = 0; i < list.Count; i++)
            {
                TreeModel treeModel = list[i];
                if (treeModel.Id.Equals(nodeID))
                {
                    list.Remove(treeModel);
                    //     MessageBox.Show("delete:"+treeModel.Id);
                    break;
                }
                else if (treeModel.Children != null && treeModel.Children.Count > 0)
                {
                    removeTreeModelByID(treeModel.Children, nodeID);
                }
            }

        }

        private void treeContextMenu_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                menuSelectAll.IsEnabled = this.ckb_showCkbox.IsChecked.Value;
                menuUnSelectAll.IsEnabled = this.ckb_showCkbox.IsChecked.Value;
            }
            catch (Exception e1) { }
        }

        //、items点击事件
        public event RoutedEventHandler MouseLeftButtonDownOnTreeModel
        {
            add { this.AddHandler(MouseLeftButtonDownOnTreeModelEvent, value); }
            remove { this.RemoveHandler(MouseLeftButtonDownOnTreeModelEvent, value); }
        }
        public static readonly RoutedEvent MouseLeftButtonDownOnTreeModelEvent = EventManager.RegisterRoutedEvent("MouseLeftButtonDownOnTreeModel", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CheckBoxTreeView));

        private void staTree_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                StackPanel sp = sender as StackPanel;
                TreeModel tt = sp.Tag as TreeModel;
                RoutedEventArgs args = new RoutedEventArgs(MouseLeftButtonDownOnTreeModelEvent, tt);
                RaiseEvent(args);
            }
            catch (Exception e1)
            {
            }
        }
        //、selectChanged事件
        public event RoutedEventHandler SelectedItemChanged
        {
            add { this.AddHandler(SelectedItemChangedEvent, value); }
            remove { this.RemoveHandler(SelectedItemChangedEvent, value); }
        }
        public static readonly RoutedEvent SelectedItemChangedEvent = EventManager.RegisterRoutedEvent("SelectedItemChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CheckBoxTreeView));

        private void tvZsmTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            try
            {
                TreeView tree = sender as TreeView;
                RoutedEventArgs args = new RoutedEventArgs(SelectedItemChangedEvent, (tree.SelectedItem));
                RaiseEvent(args);
            }
            catch (Exception e1)
            {
            }
        }

        //3、注册SelectedItem

        public TreeModel SelectItem
        {
            get { return (this.tvZsmTree.SelectedItem == null ? null : (this.tvZsmTree.SelectedItem as TreeModel)); }
        }
        public  static readonly DependencyProperty IsBtnEnabledProperty =
            DependencyProperty.Register("SelectItem", typeof(TreeModel), typeof(TreeView), new FrameworkPropertyMetadata(null, (s, e) =>
            {
                try
                {
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.Message);
                }
            }));

        private void tbox_keyWords_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                String keyword = (sender as TextBox).Text;
                if (e.Key.Equals(Key.Return) || e.Key.Equals(Key.Enter))
                {
                    //恢复节点
                    if (keyword.Length <= 0)
                    {
                        this.ItemsSourceData = _itemsSourceData;
                    }
                    //实施关键字搜索
                    else
                    {
                        getTreeModelByMC(this._itemsSourceData, keyword);
                        //list.Add(treeModel);
                        this.ItemsSourceDataForSearch = searchedItems;
                    }
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show("搜索失败，错误信息：" + e1.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        //3、注册IsShowSearchBox wxt 20190105
        public Boolean IsShowSearchBox
        {
            get { return this.ckb_showKeyWords.IsChecked.Value; }
            set
            {
                this.ckb_showKeyWords.IsChecked = value;
            }
        }
        public static readonly DependencyProperty IsShowSearchBoxProperty =
            DependencyProperty.Register("IsShowSearchBox", typeof(Boolean), typeof(CheckBoxTreeView), new PropertyMetadata(false, (s, e) =>
            {
                CheckBoxTreeView sender = s as CheckBoxTreeView;
                sender.ckb_showKeyWords.IsChecked = (Boolean)e.NewValue;
            }));

        //4、关键字
        public String KeyWords
        {
            get { return this.tbox_keyWords.Text; }
            set
            {
                this.tbox_keyWords.Text = value;
            }
        }
        public static readonly DependencyProperty KeyWordProperty =
            DependencyProperty.Register("KeyWord", typeof(String), typeof(CheckBoxTreeView), new PropertyMetadata("", (s, e) =>
            {
                CheckBoxTreeView sender = s as CheckBoxTreeView;
                sender.tbox_keyWords.Text = (String)e.NewValue;
            }));
        //4、关键字
        public Boolean IsShowOptionPanel
        {
            get { return (this.stackPanel_bottom.Visibility.Equals(Visibility.Visible)) ? true : false; }
            set
            {
                this.stackPanel_bottom.Visibility = (value ? Visibility.Visible : Visibility.Collapsed);
            }
        }
        public static readonly DependencyProperty IsShowOptionPanelProperty =
            DependencyProperty.Register("IsShowOptionPanel", typeof(Boolean), typeof(CheckBoxTreeView), new PropertyMetadata(true, (s, e) =>
            {
                CheckBoxTreeView sender = s as CheckBoxTreeView;
                sender.Visibility = ((Boolean)e.NewValue ? Visibility.Visible : Visibility.Collapsed);
            }));



        private void ckb_showKeyWords_Checked(object sender, RoutedEventArgs e)
        {
            this.grid_keywords.Visibility = Visibility.Visible;
        }

        private void ckb_showKeyWords_Unchecked(object sender, RoutedEventArgs e)
        {
            this.grid_keywords.Visibility = Visibility.Collapsed;
        }
    }
}
